package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.mapper.*;
import com.example.teachingSystem.pojo.entity.*;
import com.example.teachingSystem.pojo.form.Schedule.AddScheduleForm;
import com.example.teachingSystem.pojo.form.Schedule.SchedulePageQuery;
import com.example.teachingSystem.pojo.form.Schedule.UpdateScheduleForm;
import com.example.teachingSystem.pojo.vo.ScheduleVo;
import com.example.teachingSystem.service.CourseScheduleService;
import com.example.teachingSystem.utils.JwtHelper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import com.example.teachingSystem.utils.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86147
 * @description 针对表【course_schedule(课程安排表)】的数据库操作Service实现
 * @createDate 2023-12-11 22:55:52
 */
@Service
public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleMapper, CourseSchedule>
        implements CourseScheduleService {
    @Autowired
    private CourseScheduleMapper courseScheduleMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public Result<String> addSchedule(AddScheduleForm addScheduleForm) {
        CourseSchedule courseSchedule = new CourseSchedule();
        BeanUtils.copyProperties(addScheduleForm, courseSchedule);
        this.save(courseSchedule);
        return Result.ok(null, "添加成功");
    }

    @Override
    public Result<String> updateSchedule(UpdateScheduleForm updateScheduleForm) {
        CourseSchedule one = courseScheduleMapper.selectById(updateScheduleForm.getScheduleId());
        if (one == null) {
            Result.build(null, 500, "不存在该课程安排");
        }
        if (one != null) {
            BeanUtils.copyProperties(updateScheduleForm, one);
        }
        this.updateById(one);
        return Result.ok(null, "修改成功");
    }

    @Override
    public Result<String> deleteScheduleById(Long id) {
        CourseSchedule one = courseScheduleMapper.selectById(id);
        if (one == null) {
            Result.build(null, 500, "不存在该课程安排");
        }
        this.removeById(one);
        return Result.ok(null, "删除成功");
    }

    @Override
    public Result<List<ScheduleVo>> getMyCourseScheduleByStu(Long semesterId) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return Result.build(null, ResultCodeEnum.USER_ERROR);
        }
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getStudentId, Long.parseLong(user.getUsername())));
        if (student != null) {
            List<ScheduleVo> scheduleVoList = courseScheduleMapper.getMyCourseScheduleByStu(semesterId, student.getStudentId());
            return Result.ok(scheduleVoList);
        }
        return Result.build(null, 500, "当前用户无权限");
    }

    @Override
    public Result<List<ScheduleVo>> getMyCourseScheduleByTea(Long semesterId) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return Result.build(null, ResultCodeEnum.USER_ERROR);
        }
        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getTeacherId, Long.parseLong(user.getUsername())));
        if (teacher != null) {
            List<ScheduleVo> scheduleVoList = courseScheduleMapper.getMyCourseScheduleByTea(semesterId, teacher.getTeacherId());
            return Result.ok(scheduleVoList);
        }
        return Result.build(null, 500, "当前用户无权限");
    }

    @Override
    public PageResult<ScheduleVo> getAllCourseSchedule(SchedulePageQuery schedulePageQuery){
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return PageResult.build(null, ResultCodeEnum.USER_ERROR);
        }
        Page<CourseSchedule> page = new Page<>(schedulePageQuery.getPageNum(), schedulePageQuery.getPageSize());
        Admin admin = adminMapper.selectOne(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getAdminId, Long.parseLong(user.getUsername())));
        if (admin != null) {
            IPage<ScheduleVo> scheduleVoIPage = courseScheduleMapper.getAllCoursePage(page, schedulePageQuery);
            return PageResult.success(scheduleVoIPage);
        }
        return PageResult.build(null, 500, "当前用户无权限");
    }

}




